<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/html/js/axios.min.js"></script>
    <script src="/html/js/ajax.js"></script>
    <script src="/html/js/util.js"></script>
    <script>
        window.onload = function () {
            init(1);
        }

        async function init(pageNo) {
            let headStr="";
            let userBean=await ajaxGet("/user/getLoginUser");
            if (userBean==""){
                headStr+=`你好,请先<a href="/html/login.html">登录</a>`
            }else {
                headStr+=`欢迎您,<span style="color: red">${userBean.name}</span>`
                headStr+=`<a href="/html/custom/shop.html?userId=${userBean.id}" style="margin-left: 50px">查看购物车</a>
                      <a href="/html/custom/showOrder.html?userId=${userBean.id}" style="margin-left: 50px">我的订单</a>`
            }
            getObject("headDiv").innerHTML=headStr;



            let info = await ajaxGet("/product/findByItem", {
                pageNo: pageNo, name: getObject("name").value,
                startDate: getObject("startDate").value, endDate: getObject("endDate")
            });
            let productStr = "";
            for (let i = 0; i < info.list.length; i++) {
                let obj = info.list[i];
                productStr += `<div style="margin-left: 70px">
                               <img src='/html/img/${obj.img}' style="width: 150px;height: 150px;"><br>
                               <span>${obj.name}</span><br>
                               <span>单价:${obj.money}</span><br>
                               <span>已售出:${obj.number}</span><br>
                               <input type="button" value="查看" onclick="findProduct(${obj.id})">
                               <input type="button" value="购买" style="margin-left: 20px" onclick="add(${obj.id})">
                               </div>`
            }
            getObject("productShowDiv").innerHTML=productStr


            let pageStr="";
            for (let i=1;i<=info.pages;i++){
                pageStr+=`<a href="javascript:init(${i})" style="margin-left: 10px">${i}</a>`
            }
            getObject("pageDiv").innerHTML=pageStr;

        }

        async function findProduct(id){
            location.href=`/html/findProduct.html?id=${id}`;
        }

        async function add(id){
            let userBean=await ajaxGet("/user/getLoginUser");
            let info=await ajaxGet("/shop/add",{userId:userBean.id,productId:id})
            if (info=="ok"){
                location.href=`/html/custom/shop.html?userId=${userBean.id}`;
            }
        }
    </script>
</head>
<body>
<div id="headDiv"></div>
<div style="margin-top:50px ">
    商品名:<input type="text" id="name">
    生产起始日期:<input type="text" id="startDate">
    生产结束日期:<input type="text" id="endDate">
    <input type="button"  value="搜索" onclick="init(1)">
</div>
<div id="productShowDiv" style="display: flex;margin-top: 50px"></div>
<div id="pageDiv" style="margin-top: 20px"></div>
</body>
</html>